package com.unittec.zk.gateway.config;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;

/**
 * 三方应用采用SDK请求，网关路径是固定的，需要加入到网关的转发表中，用于后续的动态路由选择
 */
public class GatewayRouteLocatorConfig extends SimpleRouteLocator {

    public GatewayRouteLocatorConfig(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
    }

    public void refresh() {
        doRefresh();
    }

    @Override
    protected Map<String, ZuulRoute> locateRoutes() {
        Map<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulProperties.ZuulRoute>();

        routesMap.put("gateway", new ZuulRoute());
        return routesMap;
    }

    @Override
    public List<Route> getRoutes() {
        // 网关为SDK暴露的入口为http://domain:port/gateway
        List<Route> values = new ArrayList<Route>();
        values.add(new Route("gateway1", "/gateway/", "/gateway", "", true, new HashSet<String>()));
        values.add(new Route("gateway2", "/gateway", "/gateway", "", true, new HashSet<String>()));
        values.add(new Route("gateway3", "/gateway.do", "/gateway", "", true, new HashSet<String>()));
        return values;
    }
}
